package com.test.io.inputOutput;

import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutPutStreamDemo {
    // 向文件夹写入文件 如果文件不存在则自动创建
    @Test
    public void writeFile() {
        FileOutputStream fileOutputStream = null;
        try {
            // new FileOutPutStream(filePath) 方式写入数据会覆盖当前文件的数据
            // new FileOutPutStream(filePath, true) 会追加到当前文件之后
            fileOutputStream = new FileOutputStream("d:\\writeFile.text", true);
            String writeStr = "Hello,World";
            byte[] bytes = writeStr.getBytes();
            fileOutputStream.write(bytes, 0, bytes.length);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
